package interfaces.clients.dispatcher;

import exceptions.Util;
import interfaces.clients.dispatcher.commands.*;
import interfaces.clients.dispatcher.replies.*;
import interfaces.clients.rightscontrol.RightsControl;
import interfaces.clients.rightscontrol.RightsControlSystem;

import java.util.List;

/**
 * A class for dispatching user command.<br>
 * <br>
 *
 * @author Mihail A. Muhin
 */
public class Dispatcher {
	private Dispatcher() {

	}

	public static BaseReply dispatchCommand(int UID, BaseCommand cmd) {
		RightsControl rc = RightsControlSystem.getInstance();
		String s_cmd = cmd.getClass().getName();

		/*
			  this suxx is because if we'll try to write "GetFileCommand",
			  then we can have a bug after renaming the class
			*/
		if (s_cmd.equals(GetFileCommand.class.getName())) {
			GetFileCommand ccmd = (GetFileCommand) cmd;
			String contents = null;
			contents = rc.getFile(ccmd.getFilename(), ccmd.getVer());
			return new GetFileReply(contents);
		} else if (s_cmd.equals(PutFileCommand.class.getName())) {
			PutFileCommand ccmd = (PutFileCommand) cmd;
			rc.putFile(ccmd.getFilename(), ccmd.getContents());
			return new PutFileReply();
		} else if (s_cmd.equals(GetLastVersionCommand.class.getName())) {
			GetLastVersionCommand ccmd = (GetLastVersionCommand) cmd;
			int num = rc.getLastVersion(ccmd.getFilename());
			return new GetLastVersionReply(num);
		} else if (s_cmd.equals(GetFilesCommand.class.getName())) {
			GetFilesCommand ccmd = (GetFilesCommand) cmd;
			List l = rc.getFiles();
			return new GetFilesReply(l);
		} else {
			Util.LogError("Wrong user command - ".concat(s_cmd));
			return null;
		}
	}
}
